<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_vpage_ci.class.php');



class TCMS_VPage_CI_DBC extends TCMS_VPage_CI implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);
		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select type, vpage, ci_id, t_ci_id from tcms_vpage_cis where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_type($rez_obj->TYPE);
			$this->set_vpage($rez_obj->VPAGE);
			$this->set_ci_id($rez_obj->CI_ID);
			$this->set_t_ci_id($rez_obj->T_CI_ID);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_vpage_cis_seq');

		$type = $this->get_type();
		$vpage = $this->get_vpage();
		$ci_id = $this->get_ci_id();
		$t_ci_id = $this->get_t_ci_id();


		if ($id) {
			$sql = "insert into tcms_vpage_cis (id, ".
									      "type, ".
									      "vpage, ".
									      "ci_id, ".
									      "t_ci_id ".
									      ") ".
								"values ".
									      "($id,".
									      "$type, ".
									      "$vpage, ".
									      "$ci_id, ".
									      "$t_ci_id ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_vpage_cis_seq');
		}

		return $ret;
	}



	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$type = $this->get_type();
		$vpage = $this->get_vpage();
		$ci_id = $this->get_ci_id();
		$t_ci_id = $this->get_t_ci_id();

		$sql = "update tcms_vpage_cis set ".
						"type = $type, ".
						"vpage = $vpage, ".
						"ci_id = $ci_id, ".
						"t_ci_id = $t_ci_id ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function load_cis_for_page(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);
		$ret = array();

		$sql = "select id from tcms_vpage_cis where vpage = $vpage_id and t_ci_id != 0";
		$rez = $dbc->execute($sql);
		while($rez_obj = $rez->fetch_object()) {
			$tmp = new TCMS_VPage_CI_DBC();
			$tmp->load_by_id($dbc, $rez_obj->ID);
			$ret[$tmp->get_t_ci_id()] = clone $tmp;
		}


		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_vpage_cis where id = $id";
		$dbc->execute($sql);
	}


	public static function is_unique_for_page(DB_Connection $dbc, $vpage_id, $vpt_ci_id) {
		tangra_if_not_int_throw_e($vpage_id);
		tangra_if_not_int_throw_e($vpt_ci_id);

		$sql = "select id from tcms_vpage_cis where vpage = $vpage_id and t_ci_id = $vpt_ci_id";
		$rez = $dbc->execute($sql);

		return $rez->is_eod();
	}


	public static function count_cis_for_vpt_ci(DB_Connection $dbc, $vpt_ci_id) {
		tangra_if_not_int_throw_e($vpt_ci_id);

		$sql = "select count(id) as c from tcms_vpage_cis where t_ci_id = $vpt_ci_id";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();
		$ret = $rez_obj->C;

		return $ret;
	}



	public static function load_cis_for_vpt_ci(DB_Connection $dbc, $vpt_ci) {
		tangra_if_not_int_throw_e($vpt_ci);
		$ret = array();

		$sql = "select id from tcms_vpage_cis where t_ci_id = $vpt_ci ";
		$rez = $dbc->execute($sql);
		while($rez_obj = $rez->fetch_object()) {
			$tmp = new TCMS_VPage_ci_DBC();
			$tmp->load_by_id($dbc, $rez_obj->ID);
			$ret[$tmp->get_t_ci_id()] = clone $tmp;
		}


		return $ret;
	}


	public static function count_cis_for_type(DB_Connection $dbc, $type_id) {
		tangra_if_not_int_throw_e($type_id);

		$sql = "select count(id) as c from tcms_vpage_cis where type = $type_id";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();
		$ret = $rez_obj->C;

		return $ret;
	}


	public static function is_used_by_other_vpages(DB_Connection $dbc, $ci_id, $vpage_id) {
		tangra_if_not_int_throw_e($ci_id);
		tangra_if_not_int_throw_e($vpage_id);

		$sql = "select id from tcms_vpage_cis where ci_id = $ci_id and vpage <> $vpage_id";

		$rez = $dbc->execute($sql);

		return !$rez->is_eod();
	}


	public static function count_vpages_linked_to_ci(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$sql = "select count(id) as c from tcms_vpage_cis where ci_id = $ci_id";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();

		return $rez_obj->C;
	}


	public static function delete_all_for_ci(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$sql = "delete from tcms_vpage_cis where ci_id = $ci_id";
		$dbc->execute($sql);
	}



	public static function count_used(DB_Connection $dbc, $ci_id) {
		tangra_if_not_int_throw_e($ci_id);

		$sql = "select count(id) as c from tcms_vpage_cis where ci_id = $ci_id";
		$rez = $dbc->execute($sql);
		$rez_obj = $rez->fetch_object();
		$ret = $rez_obj->C;

		return $ret;
	}


	public function load_ci_by_page_and_export_name(DB_Connection $dbc, $vpage_id, $export_name) {
		tangra_if_not_int_throw_e($vpage_id);
		if (!ereg("[a-z0-9_]{1,255}", $export_name)) {
			throw new Tangra_Module_Exception('Invalid export name for ci.');
		}

		$ret = false;

		$sql = "select tcms_vpage_cis.id from tcms_vpage_cis, tcms_vpt_cis ".
						"where vpage = $vpage_id and ".
								"tcms_vpt_cis.export_name = '$export_name' and ".
								"tcms_vpt_cis.id = tcms_vpage_cis.t_ci_id";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}
}